package com.sunyur.tailor.channel.common.inner.util;

import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;

import java.util.Collection;
import java.util.Map;

/**
 * @ClassName SunyurResultUtil
 * @Author yu.chen
 * @Date 2021/4/28 22:41
 * @Version 1.0
 **/
public class SunyurResultUtil {

    /**
     * @description 判断主线接口返回是否正确
     * @param sunyurResult
     * @return boolean
     * @author yu.chen
     * @date 2021/4/28 22:45
     * @Version v1.0
     */
    public static <T> boolean isSuccess(SunyurResult<T> sunyurResult) {
        if (sunyurResult != null && sunyurResult.isSuccess() && sunyurResult.getContent() != null) {
            if (sunyurResult.getContent() instanceof Boolean) {
                return (Boolean) sunyurResult.getContent();
            } else if (sunyurResult.getContent() instanceof Collection<?>) {
                return !((Collection<?>) sunyurResult.getContent()).isEmpty();
            } else if (sunyurResult.getContent() instanceof Map<?, ?>) {
                return !((Map<?, ?>) sunyurResult.getContent()).isEmpty();
            } else if (sunyurResult.getContent() instanceof Object[]) {
                return ((Object[]) sunyurResult.getContent()).length != 0;
            }
            return true;
        }
        return false;
    }

    /**
     * @description 判断主线接口分页返回是否存在下一页
     * @param sunyurListResult
     * @return boolean
     * @author yu.chen
     * @date 2021/4/28 23:07
     * @Version v1.0
     */
    public static <T> boolean isNextPage(SunyurListResult<T> sunyurListResult) {
        return sunyurListResult.getTotalPage() != null && sunyurListResult.getTotalPage() > 1;
    }
}
